package com.deloitte.snap.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class CommonUtil {
	/**
	 * This method used to check if a string input is empty or not
	 * 
	 * @param input - The input string to check
	 * @return - Returns true if the string is empty or have length 0
	 */
	public static boolean isEmpty(String input){
		return (input == null || input.trim().length() == 0 ) ;
	}
	
	public static String getGeneratedEncodedUrl(HttpServletRequest request,HttpServletResponse response,String path){
		return  response.encodeURL(getGeneratedUrl(request,request.getContextPath(),path));
	}
	
	public static String getGeneratedUrl(HttpServletRequest request,String contextPath,String path){
		String url = "";
		StringBuffer sb = new StringBuffer();
		sb.append(contextPath);
		if(!isEmpty(path)){
			sb.append("/").append(path);
		}
		try {
			url = new URL(request.getScheme(),request.getServerName(),request.getServerPort(),sb.toString()).toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return url;
	}
	
	public static String getCurrentTime(){
		return  new Long(new Date().getTime()).toString() ;
	}
}
